#include <iostream>
#include <sys/shm.h>
#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"
 
#define image_size_max 720*1280*3
struct shareMemory {
    int pixel_rows;
    int pixel_cols;
    int pixel_channels;
    char pixel[image_size_max];
};
 
using namespace cv;
using namespace std;
struct shareMemory *get_shared_memory()
{
    int shmid = shmget((key_t)2332, sizeof(struct shareMemory), 0666|IPC_CREAT);
    if(shmid == -1)
    {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }
    //将共享内存连接到当前进程的地址空间
    void *shm = shmat(shmid, NULL, 0);
    if(shm == (void*)-1)
    {
        fprintf(stderr, "shmat failed\n");
        exit(EXIT_FAILURE);
    }
 
    struct shareMemory *shared = (struct shareMemory*)shm;
    return shared;
 
}
int main() {
 
    while(1){
    struct shareMemory *shareMemory=get_shared_memory();
    cv::Mat cvoutImg = cv::Mat(shareMemory->pixel_rows,shareMemory->pixel_cols,CV_8UC3,cv::Scalar(255, 255, 255));//bufHight,bufWidth
 
    std::cout << "width=" << shareMemory->pixel_rows<< endl
              << "height=" << shareMemory->pixel_cols << endl
              << "channel=" << shareMemory->pixel_channels << endl;
    memcpy((char*)cvoutImg.data, shareMemory->pixel,shareMemory->pixel_rows*shareMemory->pixel_cols*shareMemory->pixel_channels);
    if(!cvoutImg.data)
        break;
    cv::imshow("xx",cvoutImg);
    cv::waitKey(1);
    }
 
    return 0;
}
